package com.readygo.boot_test.service;

import com.readygo.boot_test.entity.User;
import com.readygo.boot_test.repository.UserRepository;
import com.readygo.boot_test.vo.UserCourse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.persistence.EntityManager;
import javax.persistence.Query;
import java.util.List;

@Service
public class UserService {

    private UserRepository userRepository;

    private EntityManager entityManager;

    @Autowired
    public UserService(UserRepository userRepository, EntityManager entityManager) {
        this.userRepository = userRepository;
        this.entityManager = entityManager;
    }

    public User saveOrUpdateUser(User user) {
        return userRepository.save(user);
    }

    public List<UserCourse> getUserCourseList(Integer userId) {
        Query query = entityManager.createNamedQuery("User.findUserCourseByUserId");
        query.setParameter("userId", userId);
        return query.getResultList();
    }

}
